package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.entity.MedicalCallEnd;
import com.fau.cm.entity.MedicalCallExtension;
import com.fau.cm.entity.MedicalCallPending;
import com.fau.cm.entity.MedicalCallTelInfo;
import com.fau.cm.entity.MedicalCallUntreated;
import com.fau.cm.entity.MedicalEnterpriseOutside;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.service.ICallGroupManagerService;
import com.fau.cm.util.HttpRequestUtil;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SystemConfig;

@Service
@Transactional(readOnly = true)
public class CallGroupManagerServiceImpl extends BaseService implements
		ICallGroupManagerService {
	
	@Resource
	private HttpRequestUtil httpRequestUtil;

	@Override
	public Page findCallByPage(MedicalCall call, MedicalAdmin admin,
			Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCall a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCall a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			countSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		countSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_GROUP);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0] );
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page p = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		@SuppressWarnings("unchecked")
		List<MedicalCall> list = (List<MedicalCall>) p.getResult();
		for (MedicalCall medicalCall : list) {
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())) {
				MedicalAdmin ma = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				medicalCall.setCreateUserName(ma.getName());
			}
			
			MedicalKeyValue category = this.iBaseDao.getValueByKey(medicalCall.getHospitalId(), medicalCall.getCategory());
			if(null != category){
				medicalCall.setCategoryValue(category.getKeyValue());
			}
		}
		return p;
	}

	@Override
	public Page findCallUntreatedByPage(MedicalCallUntreated call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 and a.transferUser=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallUntreated a where 1=1 and a.transferUser=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			countSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and (a.grade=? or a.grade=?) ");
		countSql.append("and (a.grade=? or a.grade=?) ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		objectList.add(SystemConfig.CALL_TASK_GRADE_GROUP);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallUntreated> list = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			MedicalCallUntreated bean = (MedicalCallUntreated) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			if(StringUtils.isNotBlank(bean.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
				bean.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
			list.add(bean);
		}
		page.setResult(list);
		return page;
	}

	@Override
	public Page findCallPendingByPage(MedicalCallPending call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallPending a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			countSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and (a.grade=? or a.grade=?) ");
		countSql.append("and (a.grade=? or a.grade=?) ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		objectList.add(SystemConfig.CALL_TASK_GRADE_GROUP);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallPending> list = new ArrayList<MedicalCallPending>();
		for(Object obj : page.getResult()) {
			MedicalCallPending bean = (MedicalCallPending) obj;
			
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			if(StringUtils.isNotBlank(bean.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
				if(null != handleUser) {
					bean.setHandleUserName(handleUser.getName());
				}
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
			list.add(bean);
		}
		return page;
	}

	@Override
	public Page findCallTransferByPage(MedicalCallUntreated call, MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 and a.transferUser <> ? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallUntreated a where 1=1 and a.transferUser <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			countSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		countSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
	
		List<MedicalCallUntreated> list = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			MedicalCallUntreated bean = (MedicalCallUntreated) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
			if(null != handleUser) {
				bean.setHandleUserName(handleUser.getName());
			}
			MedicalAdmin transferUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getTransferUser());
			if(null != transferUser) {
				bean.setTransferUserName(transferUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
			list.add(bean);
		}
		return page;
	}

	@Override
	public Page findCallEndByPage(MedicalCallEnd call, MedicalAdmin admin,
			Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallEnd a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			countSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and (a.grade=? or a.grade=?) ");
		countSql.append("and (a.grade=? or a.grade=?) ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		objectList.add(SystemConfig.CALL_TASK_GRADE_GROUP);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
	
		List<MedicalCallEnd> list = new ArrayList<MedicalCallEnd>();
		for(Object obj : page.getResult()) {
			MedicalCallEnd bean = (MedicalCallEnd) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			if(StringUtils.isNotBlank(bean.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
				bean.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
			list.add(bean);
		}
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalAdmin> findUserList(MedicalAdmin admin) {
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			return this.iBaseDao.getBySql("from MedicalAdmin a where a.enterpriseId=? and a.group.platform.platformType=? ", null, admin.getEnterpriseId(), SystemConfig.CALL_CENTER_TYPE);
		} else {
			return this.iBaseDao.getBySql("from MedicalAdmin a where a.group.id=? ", null, admin.getGroup().getId());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean callAllocate(String callsId, String usersId) {
		String callId[] = callsId.split(",");
		String userId[] = usersId.split(",");
		List<String> callIdList = new ArrayList<String>(Arrays.asList(callId));
		List<String> userIdList = new ArrayList<String>(Arrays.asList(userId));
		
		boolean flag = this.isAllocation(callId);
		if(flag) {
			for(int i=0; i<callId.length; i++){
				MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callIdList.get(i));
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, userIdList.get(i % userIdList.size()));
				call.setHandleUserId(userIdList.get(i % userIdList.size()));
				call.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
				call.setAllocationTime(new Date());
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				callUntreated.setUpdateTime(new Date());
				this.iBaseDao.addObject(callUntreated);
				this.iBaseDao.deleteObject(call);
				
				List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, callUntreated.getId());
				List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, callUntreated.getHospitalId());
				for(MedicalCallTelInfo telInfo : telInfoList) {
					for(MedicalEnterpriseOutside eOutside : eOutsideList) {
						MedicalCallExtension callExtension = new MedicalCallExtension();
						callExtension.setAttendantNo(handleUser.getUsername());
						callExtension.setCustomerNum(telInfo.getPhoneNum() + "," + eOutside.getOutSideNum());
						this.iBaseDao.saveObject(callExtension);
					}
				}
			}
			
			if(callIdList.size() > userIdList.size()) {
				for(String uId : userIdList) {
					MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, uId);
					httpRequestUtil.allocateRemind("|您有新的话务任务!", admin.getEnterpriseId(), admin.getId());
				}
			} else {
				for(int i=0; i<callId.length; i++) {
					MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, userIdList.get(i % userIdList.size()));
					httpRequestUtil.allocateRemind("|您有新的话务任务!", handleUser.getEnterpriseId(), handleUser.getId());
				}
			}
			
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * @desc 判断任务是否已经分配
	 * @param callId
	 * @return boolean
	 * @author dangjh
	 * 2015年2月7日 下午3:56:13
	 */
	private boolean isAllocation(String callId[]) {
		for(String id : callId) {
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, id);
			if(null == call) return false;
			if(!SystemConfig.CALL_TASK_GRADE_GROUP.equals(call.getGrade())) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, cId);
			call.setCategory(categoryKey);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallUntreated callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			callUntreated.setCategory(categoryKey);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			callPending.setCategory(categoryKey);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallEndCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			callEnd.setCategory(categoryKey);
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallStatus(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCall call = null;
		for(String cId : callId) {
			call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, cId);
			if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callUntreated);
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(call, callPending);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callPending);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callUntreated);	
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(call, callEnd);
				this.iBaseDao.deleteObject(call);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallUntreated callUntreated = null;
		for(String cId : callId) {
			callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callUntreated, call);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callUntreated, callPending);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callPending);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				callUntreated.setTransferUser("");
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callUntreated, callEnd);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallPending callPending = null;
		for(String cId : callId) {
			callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callPending, call);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPending, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callUntreated);				
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPending, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callUntreated);			
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callPending, callEnd);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallTransferSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallUntreated callUntreated = null;
		for(String cId : callId) {
			callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callUntreated, call);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);			
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callUntreated, callPending);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callPending);		
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callUntreated, callEnd);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallEndSta(String callIds, String statusKey) {
		String callId[] = callIds.split(",");
		MedicalCallEnd callEnd = null;
		for(String cId : callId) {
			callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(statusKey)) {
				MedicalCall call = new MedicalCall();
				BeanUtils.copyProperties(callEnd, call);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(call);
			}else if(SystemConfig.CALL_TASK_UNTREATED.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callEnd, callUntreated);
				callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callUntreated);				
			}else if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callEnd, callPending);
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callPending);		
			}else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callEnd, callUntreated);
				callUntreated.setTransferUser("");
				this.iBaseDao.deleteObject(callEnd);
				this.iBaseDao.addObject(callUntreated);		
			}else {
				break;
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delCall(MedicalCall call) {
		if(call.getId().indexOf(",") != -1){
			String[] callIds = call.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCall a where a.id=? ", callId);
			}
		}else{
			this.iBaseDao.deleteObject(call);
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delUntreatedCall(MedicalCallUntreated callUntreated) {
		if(callUntreated.getId().indexOf(",") != -1){
			String[] callIds = callUntreated.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallUntreated a where a.id=? ", callId);
			}
		}else{
			this.iBaseDao.deleteObject(callUntreated);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delPendingCall(MedicalCallPending callPending) {
		if(callPending.getId().indexOf(",") != -1){
			String[] callIds = callPending.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallPending a where a.id=? ", callId);
			}
		}else{
			this.iBaseDao.deleteObject(callPending);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delEndCall(MedicalCallEnd callEnd) {
		if(callEnd.getId().indexOf(",") != -1){
			String[] callIds = callEnd.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallEnd a where a.id=? ", callId);
			}
		}else{
			this.iBaseDao.deleteObject(callEnd);
		}
	}
	
	/*-------------------------------------------回收任务-------------------------------------------------*/

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverUntCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallUntreated callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			callUntreated.setHandleUserId("");
			callUntreated.setGrade(SystemConfig.CALL_TASK_GRADE_GROUP);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callUntreated, call);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callUntreated);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverPenCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			callPending.setHandleUserId("");
			callPending.setGrade(SystemConfig.CALL_TASK_GRADE_GROUP);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callPending, call);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callPending);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recoverEndCall(String callsId) {
		String callId[] = callsId.split(",");
		for(String cId : callId) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			callEnd.setHandleUserId("");
			callEnd.setGrade(SystemConfig.CALL_TASK_GRADE_GROUP);
			MedicalCall call = new MedicalCall();
			BeanUtils.copyProperties(callEnd, call);
			call.setIncomingCount(0);
			this.iBaseDao.addObject(call);
			this.iBaseDao.deleteObject(callEnd);
			
			List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, call.getId());
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, call.getHospitalId());
			for(MedicalCallTelInfo telInfo : telInfoList) {
				for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
					this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportAll(MedicalAdmin admin,MedicalCall call) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCall a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_GROUP);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCall> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCall medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}
	
	@Override
	public Workbook exportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCall medicalCall = (MedicalCall) this.iBaseDao
						.getObject(MedicalCall.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook untExportAll(MedicalAdmin admin, MedicalCallUntreated call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where a.transferUser=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallUntreated> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallUntreated medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook untExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallUntreated medicalCall = (MedicalCallUntreated) this.iBaseDao
						.getObject(MedicalCallUntreated.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook penExportAll(MedicalAdmin admin, MedicalCallPending call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where 1=1 ");

		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallPending> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallPending medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook penExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallPending medicalCall = (MedicalCallPending) this.iBaseDao
						.getObject(MedicalCallPending.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook traExportAll(MedicalAdmin admin, MedicalCallUntreated call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where a.transferUser <> ? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(SystemConfig.CALL_UNT_NO_TRANSFER);
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");

		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallUntreated> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallUntreated medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook traExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallUntreated medicalCall = (MedicalCallUntreated) this.iBaseDao
						.getObject(MedicalCallUntreated.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook endExportAll(MedicalAdmin admin, MedicalCallEnd call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? ");
			objectList.add(admin.getEnterpriseId());
		} else {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		statusSql.append("and a.grade=? ");
		objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");

		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallEnd> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallEnd medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook endExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallEnd medicalCall = (MedicalCallEnd) this.iBaseDao
						.getObject(MedicalCallEnd.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

}
